import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mvvm_provider/base/consumer_status_widget.dart';
import 'package:mvvm_provider/const/theme_provider.dart';
import 'package:mvvm_provider/page/home/states/home_state.dart';
import 'package:provider/provider.dart';
import '../../../base/base_grid_view.dart';
import '../../../base/base_stateful_page.dart';
import '../../../base/consumer_widget.dart';
import '../../../routers/home_router.dart';
import '../../../routers/navigator_utils.dart';
import '../view_model/home_view_model.dart';
import '../widgets/car_toon_widget.dart';

class HomePage extends BasePage {
  const HomePage({super.key});

  @override
  BasePageState<BasePage> getState() => _HomePageState();
}

class _HomePageState extends BasePageState<HomePage> {
  @override
  void initState() {
    super.initState();
    super.pageTitle = '首页';
    isBack = false;
  }

  @override
  void buildComplete() {
    super.buildComplete();
    _onRefresh();
  }

  @override
  Widget left() {
    return const SizedBox();
  }

  /// 上拉加载
  void _onLoading() {
    getListData();
  }

  /// 下拉刷新
  void _onRefresh() {
    getListData(isRefresh: true);
  }

  void getListData({bool isRefresh = false}) {
    getViewModel<HomeViewModel>().getListData(isRefresh);
  }

  /// 暗黑模式
  Widget _blackWidget() {
    final themeProvider = context.watch<ThemeProvider>();
    return Container(
      height: 60.h,
      width: 1.sw,
      color: Colors.blue,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            !themeProvider.isDarkMode ? '打开暗黑模式' : '关闭暗黑模式',
            style: TextStyle(fontSize: 12.sp, color: Colors.white, fontWeight: FontWeight.w500),
          ),
          Switch(
              activeColor: Colors.greenAccent,
              value: themeProvider.isDarkMode,
              onChanged: (bool change) {
                final newMode = themeProvider.isDarkMode ? ThemeMode.light : ThemeMode.dark;
                themeProvider.setThemeMode(newMode);
              }),
          InkWell(
              onTap: () {
                NavigatorUtils.push(
                  context,
                  HomeRouter.streamProviderPage,
                );
              },
              child: Text(
                'StreamProvider案例',
                style: TextStyle(fontSize: 12.sp, color: Colors.white, fontWeight: FontWeight.w500),
              ))
        ],
      ),
    );
  }

  @override
  Widget buildPage(BuildContext context) {
    return Column(
      children: [
        _blackWidget(),
        Expanded(
          child: ConsumerStatusWidget<HomeViewModel, HomeState>(
            placeHolderType: PlaceHolderType.gridViewPlaceHolder,
            builder: (context, state, child) {
              return BaseGridView(
                enablePullDown: true,
                enablePullUp: true,
                onRefresh: _onRefresh,
                onLoading: _onLoading,
                refreshController: getViewModel<HomeViewModel>().refreshController,
                scrollController: getViewModel<HomeViewModel>().scrollController,
                data: state.dataList ?? [],
                padding: EdgeInsets.all(10.h),
                childAspectRatio: 0.7,
                crossAxisSpacing: 10.w,
                mainAxisSpacing: 10.h,
                crossAxisCount: 2,
                bgColor: const Color(0xFFF3F4F8),
                itemBuilder: (context22, index) {
                  return CarToonWidget(
                    index: index,
                    model: state.dataList![index],
                    onTap: () async {
                      NavigatorUtils.push(context, HomeRouter.homeDetailPage,
                          arguments: {"imageUrl": state.dataList?[index].image});
                    },
                  );
                },
              );
            },
          ),
        )
      ],
    );
  }
}
